﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Diagnostics;
using DataCenter.Clr;
using DataCenter.Share;
using Microsoft.SqlServer.Server;

public partial class StoredProcedures
{
    [SqlProcedure]
    public static SqlInt32 AggregateValue(SqlString ObjectId, SqlString ValueType, SqlString OperType, SqlDateTime StartTime, SqlDateTime EndTime, SqlInt32 Step, SqlString GroupStyle, SqlString AggregateStyle, SqlString Parameters)
    {
        if (AggregateStyle.IsNull)
            throw new ArgumentNullException("参数 AggregateStyle 不能为空");

        Stopwatch watch = new Stopwatch();
        watch.Start();

        CenterEntry entry = new CenterEntry();
        int result = entry.Aggregate(ObjectId.IsNull ? "" : ObjectId.Value,
                                     ValueType.IsNull ? "" : ValueType.Value,
                                     OperType.IsNull ? "" : OperType.Value,
                                     StartTime.IsNull ? Utils.EMPTY_TIME : StartTime.Value,
                                     EndTime.IsNull ? DateTime.MaxValue : EndTime.Value,
                                     Step.IsNull ? 1 : (Step.Value <= 0 ? 1 : Step.Value),
                                     GroupStyle.IsNull ? "" : GroupStyle.Value,
                                     AggregateStyle.Value, 
                                     Parameters.IsNull ? "" : Parameters.Value);

        watch.Stop();
        SqlContext.Pipe.Send("查询时间：" + watch.ElapsedMilliseconds);

        return result;
    }

    [SqlProcedure]
    public static SqlInt32 CalculateValue(SqlString ObjectId, SqlDateTime StartTime, SqlDateTime EndTime)
    {
        Stopwatch watch = new Stopwatch();
        watch.Start();

        CenterEntry entry = new CenterEntry();
        int result = entry.CallApp(ExecTypes.Import | ExecTypes.Process | ExecTypes.Calculate,
                                   ObjectId.IsNull ? "" : ObjectId.Value,
                                   StartTime.IsNull ? Utils.EMPTY_TIME : StartTime.Value,
                                   EndTime.IsNull ? DateTime.MaxValue : EndTime.Value);
        watch.Stop();
        SqlContext.Pipe.Send("计算用时：" + watch.ElapsedMilliseconds);

        return result;
    }

    [SqlProcedure]
    public static SqlInt32 DropValue(SqlString ObjectId, SqlString ValueType, SqlDateTime StartTime, SqlDateTime EndTime)
    {
        Stopwatch watch = new Stopwatch();
        watch.Start();

        CenterEntry entry = new CenterEntry();
        int result = entry.Delete(ObjectId.IsNull ? "" : ObjectId.Value,
                                  ValueType.IsNull ? "" : ValueType.Value,
                                  StartTime.IsNull ? Utils.EMPTY_TIME : StartTime.Value,
                                  EndTime.IsNull ? DateTime.MaxValue : EndTime.Value);

        watch.Stop();
        SqlContext.Pipe.Send("删除时间：" + watch.ElapsedMilliseconds);

        return result;
    }

    [SqlProcedure]
    public static SqlInt32 ImportValue(SqlString ObjectId, SqlDateTime StartTime, SqlDateTime EndTime)
    {
        Stopwatch watch = new Stopwatch();
        watch.Start();

        CenterEntry entry = new CenterEntry();
        int result = entry.CallApp(ExecTypes.Import,
                                   ObjectId.IsNull ? "" : ObjectId.Value,
                                   StartTime.IsNull ? Utils.EMPTY_TIME : StartTime.Value,
                                   EndTime.IsNull ? DateTime.MaxValue : EndTime.Value);
        watch.Stop();
        SqlContext.Pipe.Send("导入用时：" + watch.ElapsedMilliseconds);

        return result;
    }

    [SqlProcedure]
    public static SqlInt32 InsertValue(SqlInt32 ObjectId, SqlInt32 ValueType, SqlInt32 OperType, SqlDateTime ValueTime, SqlSingle Value)
    {
        if (ValueTime.IsNull)
            throw new ArgumentNullException("参数 ValueTime 不能为空");
        if (Value.IsNull)
            throw new ArgumentNullException("参数 Value 不能为空");

        Stopwatch watch = new Stopwatch();
        watch.Start();

        CenterEntry entry = new CenterEntry();
        int result = entry.Insert(ObjectId.IsNull ? 0 : ObjectId.Value,
                                  ValueType.IsNull ? 0 : ValueType.Value,
                                  OperType.IsNull ? 1 : OperType.Value,
                                  ValueTime.Value,
                                  Value.Value);

        watch.Stop();        
        SqlContext.Pipe.Send("插入时间：" + watch.ElapsedMilliseconds);

        return result;
    }

    [SqlProcedure]
    public static SqlInt32 ProcessValue(SqlString ObjectId, SqlDateTime StartTime, SqlDateTime EndTime)
    {
        Stopwatch watch = new Stopwatch();
        watch.Start();

        CenterEntry entry = new CenterEntry();
        int result = entry.CallApp(ExecTypes.Import | ExecTypes.Process,
                                   ObjectId.IsNull ? "" : ObjectId.Value,
                                   StartTime.IsNull ? Utils.EMPTY_TIME : StartTime.Value,
                                   EndTime.IsNull ? DateTime.MaxValue : EndTime.Value);
        watch.Stop();
        SqlContext.Pipe.Send("处理用时：" + watch.ElapsedMilliseconds);

        return result;
    }

    [SqlProcedure]
    public static SqlInt32 QueryValue(SqlString ObjectId, SqlString ValueType, SqlString OperType, SqlDateTime StartTime, SqlDateTime EndTime, SqlInt32 Step, SqlInt32 ImportValue)
    {
        int result = CenterEntry.SUCCESS;

        CenterEntry entry = new CenterEntry(1);

        if (!ImportValue.IsNull && ImportValue.Value == 1)
        {
            Stopwatch watch1 = new Stopwatch();
            watch1.Start();

            result = entry.CallApp(ExecTypes.Import | ExecTypes.Process | ExecTypes.Calculate,
                                   ObjectId.IsNull ? "" : ObjectId.Value,
                                   StartTime.IsNull ? Utils.EMPTY_TIME : StartTime.Value,
                                   EndTime.IsNull ? DateTime.MaxValue : EndTime.Value);
            watch1.Stop();
            SqlContext.Pipe.Send("处理用时：" + watch1.ElapsedMilliseconds);
        }

        if (result != CenterEntry.ERROR)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();

            result = entry.Query(ObjectId.IsNull ? "" : ObjectId.Value,
                                 ValueType.IsNull ? "" : ValueType.Value,
                                 OperType.IsNull ? "" : OperType.Value,
                                 StartTime.IsNull ? Utils.EMPTY_TIME : StartTime.Value,
                                 EndTime.IsNull ? DateTime.MaxValue : EndTime.Value,
                                 Step.IsNull ? 1 : (Step.Value <= 0 ? 1 : Step.Value));

            watch.Stop();
            SqlContext.Pipe.Send("查询时间：" + watch.ElapsedMilliseconds);
        }

        return result;
    }

    [SqlProcedure]
    public static SqlInt32 QueryRecentValue(SqlString ObjectId, SqlString ValueType)
    {
        CenterEntry entry = new CenterEntry(1);
        return entry.QueryRecentValue(ObjectId.IsNull ? "" : ObjectId.Value,
                                      ValueType.IsNull ? "" : ValueType.Value);
    }

    [SqlProcedure]
    public static SqlInt32 QueryObjectInfo(SqlString ObjectId, SqlString ObjectType)
    {
        CenterEntry entry = new CenterEntry();
        return entry.QueryInfo(ObjectId.IsNull ? "" : ObjectId.Value,
                               ObjectType.IsNull ? "" : ObjectType.Value);
    }

    [SqlProcedure]
    public static SqlInt32 QueryLastValue(SqlString ObjectId, SqlString ValueType)
    {
        if (ObjectId.IsNull || string.IsNullOrEmpty(ObjectId.Value))
            throw new ArgumentNullException("参数 ObjectId 不能为空");
        if (ValueType.IsNull || string.IsNullOrEmpty(ValueType.Value))
            throw new ArgumentNullException("参数 ValueType 不能为空");
    
        Stopwatch watch = new Stopwatch();
        watch.Start();

        CenterEntry entry = new CenterEntry(1);
        int result = entry.QueryLastValue(ObjectId.Value, ValueType.Value);

        watch.Stop();
        SqlContext.Pipe.Send("查询时间：" + watch.ElapsedMilliseconds);

        return result;
    }

    [SqlProcedure]
    public static SqlInt32 RefreshSetting()
    {
        return CenterEntry.RefreshSetting();
    }
};
